<?php
/**
*Draw a slideshow of images.
*@author Willy Gardiol <willy@gardiol.org>
*@version 1.0
*@package Boxes
*@subpackage Slideshow
*/
class SlideShow extends Box
{
	public function __construct( $title )
	{
		parent::__construct( $title, "inline" );
		$this->SetStorage(array("path" => "",
							"delay" => 5000,
							"speed" => 2000,
							"width" => "auto",
							"random" => false,
							"height" => "auto" ) );
	}
	public function GetImages()
	{
		$images = $this->Get("path");
		if ( $images !== false )
		{
			$images = Storage::Get($images);
			if ( $images !== false )
			{
				$ret = array();
				foreach ( $images->GetLeaves() as $k => $image )
				{
					$data = $image->GetDetails();
					if ( $data["type"] == "img" )
						$ret[] = (string)$image;
				}
				if ( count( $ret ) > 0 )
					$images = $ret;
				else
					$images = false;
			}
		}
		if ( $this->Get("random") )
			shuffle( $images );
		return $images;
	}
	protected function GenerateHtml()
	{
		Core::AddJsScript("Engine/plugins/boxes/slideshow/slideshow.js");
		$this->AddControl( "SlideShowQuery", I18N("Configure slideshow options...") );
		$this->AddControl( "SlideShowImgQuery", I18N("Select stored images...") );


		$width = $this->Get("width");
		$height = $this->Get("height");

		if ( ($items = $this->GetImages()) !== false )
		{
			$style = new Css( array("width" => $width,
								"height" => $height) );

			$img = new HtmlImg( reset( $items ), I18N( $this->GetTitle() ), array("style" => $style) );
			# we need the ID for the JS part
			$id = $img->GetId();

			$img->JsInit("SlideShow", array( $this->Get("delay"),
										$this->Get("speed"),
										$this->GetId() ) );

			return $img;
		}
		return I18N("Please select a storage ares for the slideshow");
	}
}
/**
*Return to the client side the list of images composing the slideshow.
*@author Willy Gardiol <willy@gardiol.org>
*@version 1.0
*@package Boxes
*@subpackage Slideshow
*/
class SlideshowList extends CoreRequestJson
{
	public function Process()
	{
		$atom_id = Core::GetVar("atom_id", false, "string");

		$images = false;
		if ( ($box = AtomCache::GetAtomData( $atom_id )) !== false )
			$images = $box->GetImages();
		$this->SetOutput( "images", $images);
	}
}
/**
*Configure a slideshow of images.
*@author Willy Gardiol <willy@gardiol.org>
*@version 1.0
*@package Boxes
*@subpackage Slideshow
*/
class SlideShowQuery extends BoxEdit
{
	protected function BoxEdit( $box )
	{
		$html = new HtmlEditRequest();

		$html->Append( $fs = new HtmlFieldset(I18N("Slideshow delay")) );
		$fs->Append( new HtmlTextInput("","ss_delay", $box->Get("delay")));

		$html->Append( $fs = new HtmlFieldset(I18N("Slideshow fade-in speed")) );
		$fs->Append( new HtmlTextInput("","ss_speed", $box->Get("speed")));

		$html->Append( $fs = new HtmlFieldset(I18N("Images width")) );
		$fs->Append( new HtmlTextInput("","ss_width", $box->Get("width")));

		$html->Append( $fs = new HtmlFieldset(I18N("Images height")) );
		$fs->Append( new HtmlTextInput("","ss_height", $box->Get("height")));

		$html->Append( $fs = new HtmlFieldset(I18N("Configuation options")) );
		$fs->Append( new HtmlCheckboxInput(I18N("Random order"),"ss_random", $box->Get("random")));

		$html->AppendSubmit(I18N("Confirm"));
		return $html;
	}
	protected function BoxSave( $box )
	{
		$text = Core::PostVar("ss_delay", $box->Get("delay"), "string" );
		$box->Set("delay",$text ) && $this->SetReload(true);

		$text = Core::PostVar("ss_speed", $box->Get("speed"), "string" );
		$box->Set("speed",$text ) && $this->SetReload(true);

		$text = Core::PostVar("ss_width", $box->Get("width"), "string" );
		$box->Set("width",$text ) && $this->SetReload(true);

		$text = Core::PostVar("ss_height", $box->Get("height"), "string" );
		$box->Set("height",$text ) && $this->SetReload(true);

		$text = Core::PostVar("ss_random", $box->Get("random"), "bool" );
		$box->Set("random",$text ) && $this->SetReload(true);

		return I18N("Slideshow configured!");
	}
}
/**
*Configure the storage source for a slideshow of images.
*@author Willy Gardiol <willy@gardiol.org>
*@version 1.0
*@package Boxes
*@subpackage Slideshow
*/
class SlideShowImgQuery extends BoxEdit
{
	protected function BoxEdit( $box )
	{
		$html = new HtmlEditRequest();
		$html->Append( new HtmlStorageSelector( "path", $box->Get("path") ) );
		$html->AppendSubmit(I18N("Save selected storage"));
		return $html;
	}
	protected function BoxSave( $box )
	{
		$path = Core::PostVar("path", $box->Get("path"), "string" );
		$box->Set("path",$path ) && $this->SetReload(true);
		return I18N("New path ($path) has been saved");
	}
}
?>